<template>
  <div>
    <el-upload
      action="http://linweiqin.cn:8001/v1/addimg/avatar"
      :show-file-list="false"
      :on-success="success"
      :before-upload="upload">
      <img v-if="imgU" :src="imgU" alt="" />
      <i v-else class="el-icon-plus upicon"></i>
    </el-upload>
  </div>
</template>
<script>
export default {
  components: {},
  data() {
    return {
      imgU: "",
    };
  },
  methods: {
    success: function (res) {
      let imgU = "http://linweiqin.cn:8001/img/" + res.image_path;
      this.imgU = imgU;
    },
    upload: function (file) {
      let sunM = file.size / 1024 / 1024; 
      let imgT = file.type.trim(); 
      let liM = size_M > 2 ? false : true;
      if (!liM) {
        this.$message("上传文件不能超过2M");
      }
      let vIt = true;
      if (imgT == "image/png" || imgT == "image/jpeg") {
        vIt = true;
      } else {
        vIt = false;
        this.$message({
          type: "warning",
          message: "上传文件格式不正确！",
        });
      }
      return liM && vIt; 
    },
  },
};
</script>
<style lang='less' scoped>
.upicon {
  font-size: 28px;
  color: #8c939d;
  width: 110px;
  height: 110px;
  line-height: 110px;
  text-align: center;
  border: 1px solid #666;
}
</style>